package com.mofei.data.linkedlist.simple;

import com.sun.tools.javap.TypeAnnotationWriter;

public class MySimpleList<T> {

    /**
     * 头结点
     */
    Node<T> head;

    public boolean add(T data) {
        if (data == null) {
            return false;
        }
        Node<T> node = new Node<>();
        node.setData(data);
        if (head == null) {
            head = node;
            return true;
        }
        //在头部插入
        node.next = head;
        head = node;
        return true;
    }

    public boolean remove(T data) {
        if (data == null) {
            return false;
        }
        if (head == null) {
            return false;
        }
        if (data.equals(head.data)) {
            head = head.next;
            return true;
        }
        Node currentNode = head;
        while (true) {
            if (currentNode == null) {
                return false;
            }
            if (currentNode.next != null && data.equals(currentNode.next.getData())) {
                currentNode.next = currentNode.next.next;
                return true;
            }
            currentNode = currentNode.next;
        }
    }
}
